package com.GlobalPaint.app.ui.Home.friend;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.FriendsActivityAdapter;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.EventBusben;
import com.GlobalPaint.app.bean.MyFriendBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.bean.codeBean;
import com.GlobalPaint.app.bean.qunlianBean;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jzy.message.activities.base.BaseApplication;
import com.jzy.message.util.xmpp.XmppUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;



public class QunLiaoActivity extends AppCompatActivity {

    @BindView(R.id.iv_goback)
    ImageView ivGoback;
    @BindView(R.id.tv_common_center)
    TextView tvCommonCenter;
    @BindView(R.id.tv_fresh)
    TextView tvFresh;
    @BindView(R.id.ed_qunName)
    EditText edQunName;
    @BindView(R.id.tv_hy)
    TextView tvHy;
    @BindView(R.id.xrecycler)
    XRecyclerView xrecycler;
    private LinearLayoutManager linearLayoutManager;
    private MyFriendBean bean;
    private codeBean codebean;
    private List<MyFriendBean.DataBean> list;
    private FriendsActivityAdapter adapter;
    private List list_temp=new ArrayList();
    private qunlianBean qunlianBean;
    private qunlianBean qunlianBeanchengyuan;
    private String ids = String.valueOf(DataManager.userEntity.getUserId());
    public static final int CMD_USER_QUN = 0X0001; //新建群
    public static final int CMD_USER_QUNCHENGYUAN = 0X0002;//群成员
    private Handler mhandler=new Handler(){
        public void handleMessage(Message message){
            switch (message.what) {
                case CMD_USER_QUN:
                    //上传第一遍  传房间名字和群主
                    String url= Constants.JASON_SERVICE_URL+"qlxroomuserrelation/saveById";
                    DataManager.getInst().postHttpRequestJsonClass(url, DataManager.userEntity.getCookie(),qunlianBean , codeBean.class, new DataManager.Callback() {
                        @Override
                        public void onSuccess(Object o) {
                            codebean = (codeBean) o;
                            if (codebean.getStatus() == 1) {
                                if(codebean.getMsg().equals("该房间已存在")){
                                    Toast.makeText(QunLiaoActivity.this, "该房间已存在", Toast.LENGTH_LONG).show();
                                     return;
                                }else {
                                    mhandler.sendEmptyMessage(CMD_USER_QUNCHENGYUAN);
                                }
                            }
                        }

                        @Override
                        public void onFailure(ReturnStatusResultEntity error) {
                            Toast.makeText(QunLiaoActivity.this, "创建失败", Toast.LENGTH_LONG).show();
                        }
                    });
                    break;
                case CMD_USER_QUNCHENGYUAN:
                    //上传第一遍  传房间名字和群主
                    String url2= Constants.JASON_SERVICE_URL+"qlxroomuserrelation/saveById";
                    DataManager.getInst().postHttpRequestJsonClass(url2, DataManager.userEntity.getCookie(),qunlianBeanchengyuan , codeBean.class, new DataManager.Callback() {
                        @Override
                        public void onSuccess(Object o) {
                            codebean = (codeBean) o;
                            if (codebean.getStatus() == 1) {
                                if(codebean.getMsg().equals("该房间已存在")){
                                    Toast.makeText(QunLiaoActivity.this, "该房间已存在", Toast.LENGTH_LONG).show();
                                }else {
                                    EventBusben baseMessage = new EventBusben("qunliao");
                                    EventBus.getDefault().post(baseMessage);
                                    Toast.makeText(QunLiaoActivity.this, "创建成功", Toast.LENGTH_LONG).show();
                                    XmppUtil.createRoom(BaseApplication.xmppConnection,edQunName.getText().toString(),"");
                                    finish();
                                }

                            }
                        }

                        @Override
                        public void onFailure(ReturnStatusResultEntity error) {
                            Toast.makeText(QunLiaoActivity.this, "创建失败", Toast.LENGTH_LONG).show();
                        }
                    });
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qun_liao);
        ButterKnife.bind(this);
        initView();

    }

    private void initView() {
        tvFresh.setVisibility(View.VISIBLE);
        tvFresh.setText("创建");
        adapter=new FriendsActivityAdapter(list);
        linearLayoutManager=new LinearLayoutManager(getApplicationContext());
        xrecycler.setLayoutManager(linearLayoutManager);
        xrecycler.setLoadingMoreEnabled(false);
        xrecycler.setPullRefreshEnabled(false);
    }
    private void getFriends() {
        String url= Constants.JASON_SERVICE_URL+"appFriends/getFriends?userName="+ DataManager.userEntity.getUserName();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), MyFriendBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean=(MyFriendBean)o;
                if (bean.getStatus() == 1){
                    if(bean.getData() != null && bean.getData().size()>0){
                        list=bean.getData();
                        adapter=new FriendsActivityAdapter(list);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();

                    }else {
                        list=new ArrayList<MyFriendBean.DataBean>();
                        adapter=new FriendsActivityAdapter(list);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(QunLiaoActivity.this,bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                xrecycler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                if(bean !=null){
                    if(bean.getData() != null && bean.getData().size()>0) {
                        bean.getData().clear();
                        Toast.makeText(QunLiaoActivity.this, bean.getMsg(), Toast.LENGTH_LONG).show();
                        adapter.notifyDataSetChanged();
                        xrecycler.refreshComplete();
                    }
                }
            }
        });
    }
    private void ChuangJian(){
        if(TextUtils.isEmpty(edQunName.getText().toString())){
            Toast.makeText(QunLiaoActivity.this, "请输入群名", Toast.LENGTH_SHORT).show();
            return;
        }

            if(adapter.list_temp==null){
                return;
            }
        list_temp= adapter.list_temp;
        if(list_temp.size()==0){
            Toast.makeText(QunLiaoActivity.this, "请选择好友", Toast.LENGTH_SHORT).show();
            return;
        }
        if(list_temp.size()>0){
            for(int i=0;i<list_temp.size();i++){
                ids+=","+list_temp.get(i);
            }
        }

        String ids1=ids;
        Log.d("dddd", "ChuangJian: "+ids1);
        //群主自己的信息
        qunlianBean=new qunlianBean();
        qunlianBean.setDistinguish(1);
        qunlianBean.setRoomName(edQunName.getText().toString());
        qunlianBean.setIds(String.valueOf(DataManager.userEntity.getUserId()));
        ids="";
        //群成员
        qunlianBeanchengyuan=new qunlianBean();
        qunlianBeanchengyuan.setDistinguish(2);
        qunlianBeanchengyuan.setRoomName(edQunName.getText().toString());
        qunlianBeanchengyuan.setIds(ids1);
        ids="";
        mhandler.sendEmptyMessage(CMD_USER_QUN);
    }
    @OnClick({R.id.iv_goback, R.id.tv_fresh, R.id.tv_hy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_goback:
                finish();
                break;
            case R.id.tv_fresh:
                ChuangJian();
                break;
            case R.id.tv_hy:
                getFriends();
                break;
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
